public class DpDemo {
    public static void main(String[] args) {
        String A = "bc";
        int x = getLongestPalindrome(A);
        System.out.println("x = " + x);
    }

    private static int getLongestPalindrome(String A) {
        int l,r;
        char[] chars = A.toCharArray();
        int maxLen = 0;
        if(chars.length ==1) return 1;
        for (int i = 0; i < chars.length; i++) {
            int len = 0;
            l = i;
            r = chars.length-1;
            while (l<r){
                if(chars[r]==chars[l]) {
                    int lIndex = l,rIndex = r;
                    while (lIndex<rIndex && chars[lIndex] == chars[rIndex]){
                        lIndex++;rIndex--;len+=2;
                    }
                    if(lIndex >= rIndex){if(lIndex == rIndex) len++; maxLen = Math.max(maxLen,len);break;}
                    else len = 0;
                }
                r--;
            }
            if(l >= r && maxLen==0) maxLen=1;
        }
        return maxLen;
    }
}
